package com.wasu.config;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

@Configuration
public class RedisConfig {
	
	@Value("${spring.redis.cluster.nodes}")
	private String clusterNodes;
	
	@Bean
	public JedisCluster getJedisCluster() {
		// 分割集群节点
		String[] cNodes = clusterNodes.split(",");
		Set<HostAndPort> hostAndPorts = new HashSet<>();
		// 循环节点对象
		for (String nodes : cNodes) {
			String[] hostPorts = nodes.split(":");
			hostAndPorts.add(new HostAndPort(hostPorts[0], Integer.parseInt(hostPorts[1])));
		}
		// 创建 Redis 集群对象
		JedisCluster jedisCluster = new JedisCluster(hostAndPorts);
		return jedisCluster;
	}
	
}
